home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DDJ0192.ARJ / FILEIO.C < prev    next >
C/C++ Source or Header  |  1991-09-04  |  4KB  |  177 lines

  1. /****************************************************************
  2.  *                                                              *
  3.  * FILEIO.C File oriented I/O routines for CB386                *
  4.  * See makefile for compile directives                          *
  5.  * Al Williams -- August 1991                                   *
  6.  *                                                              *
  7.  ****************************************************************/
  8. #include <stdio.h>
  9. #include <malloc.h>
  10. #include <errno.h>
  11. #include "cb386.h"
  12. #include "display.h"
  13.  
  14. void (*slbreak)();   /* place to hook break handler */
  15.  
  16. /* ^C handler when saving -- used in DISKIO.C too */
  17. void save_break()
  18.   {
  19.   advise("\aOperation aborted");
  20.   if (slbreak) slbreak();
  21.   when_break=slbreak;
  22.   }
  23.  
  24. /* ^C handler when loading -- used in DISKIO.C too */
  25. void load_break()
  26.   {
  27.   cleanup();
  28.   when_break=slbreak;
  29.   }
  30.  
  31. /* Clean up an aborted load */
  32. cleanup()
  33.   {
  34.   if (diskbuf) free(diskbuf);
  35.   diskbuf=NULL;
  36.   memset(&bufinfo,0,sizeof(bufinfo));
  37.   strcpy(bufinfo.title,"<EMPTY>");
  38.   strcpy(bufinfo.source,"N/A");
  39.   }
  40.  
  41. /* save disk image to a file */
  42. m_save()
  43.   {
  44.   slbreak=when_break;
  45.   when_break=save_break;
  46.   saveit();
  47.   when_break=slbreak;
  48.   slbreak=NULL;
  49.   }
  50.  
  51. /* load disk image to a file */
  52. m_load()
  53.   {
  54.   slbreak=when_break;
  55.   when_break=load_break;
  56.   loadit();
  57.   when_break=slbreak;
  58.   slbreak=NULL;
  59.   }
  60.  
  61. /* actual routine to save data */
  62. saveit()
  63.   {
  64.   char fn[66];
  65.   FILE *f=NULL;
  66.   int cnt1;
  67. /* if no data to save, forget it */
  68.   if (!diskbuf)
  69.     {
  70.     advise("No disk image in memory");
  71.     return;
  72.     }
  73. /* get a file name */
  74.   if (!getfilen(fn,65)) return;
  75.   critical_err=0;
  76. /* check if the file already exists */
  77.   if (!access(fn,0))
  78.     {
  79.     if (prompt("\aFile exists. Overwrite? (Y/N)"
  80.                ,"NY",TEXTCOLOR)<=0)
  81.       return;
  82.     }
  83. /* if checking for the file caused a critical error,
  84.    don't even try to open the file */
  85.   if (!critical_err) f=fopen(fn,"wb");
  86.  
  87. /* if the file can't be opened (or wasn't because of
  88.    a critical error, forget it */
  89.   if (!f)
  90.     {
  91.     advise("Can't open file for writing");
  92.     return;
  93.     }
  94.  
  95. /* Get a title */
  96.   if (ask("Title: ",NULL,TEXTCOLOR,64,fn,NULL)==-1) return;
  97. /* only change title if new one was entered */
  98.   if (*fn) strcpy(bufinfo.title,fn);
  99. /* Turn the wait indicator on */
  100.   wait_on();
  101.   /* set dirty bit in image. copies/source/csum are meaningless */
  102.   bufinfo.dirty=1;
  103. /* write signature */
  104.   putw('C386',f);
  105. /* write size of bufinfo */
  106.   putw(sizeof(struct _bufinfo),f);
  107. /* write bufinfo */
  108.   fwrite(&bufinfo,1,sizeof(struct _bufinfo),f);
  109. /* write entire buffer in one swoop */
  110.   cnt1=fwrite(diskbuf,1,bufinfo.size,f);
  111. /* one | is deliberate here -- we always want to fclose */
  112.   if (ferror(f)|fclose(f)|(cnt1!=bufinfo.size))
  113.     advise("\aError writing to file");
  114.   else
  115.     bufinfo.dirty=0;
  116.   wait_off();
  117.   }
  118.  
  119. /* routine to actually load file to disk image */
  120. loadit()
  121.   {
  122.   char fn[66];
  123.   FILE *f;
  124.   int cnt,cnt1;
  125. /* if buffer is full, confirm */
  126.   if (!dirtyquery()) return;
  127. /* get file name */
  128.   if (!getfilen(fn,65)) return;
  129. /* open it */
  130.   f=fopen(fn,"rb");
  131. /* can't open it, forget it */
  132.   if (!f)
  133.     {
  134.     advise("Can't open file");
  135.     return;
  136.     }
  137. /* Turn wait indicator on */
  138.   wait_on();
  139. /* check file type */
  140.   cnt=getw(f);
  141.   if (cnt!='C386')
  142.     {
  143.     advise("Not a C386 image file");
  144.     return;
  145.     }
  146. /* get size of bufinfo structure */
  147.   cnt=getw(f);
  148. /* new versions of C386 can't make bufinfo smaller, only larger
  149.    at the end */
  150.   fread(&bufinfo,1,cnt,f);
  151. /* kill old image */
  152.   if (diskbuf) free(diskbuf);
  153. /* allocate new image based on size */
  154.   diskbuf=malloc(bufinfo.size);
  155.   if (!diskbuf)
  156.     {
  157.     advise("Insufficient memory");
  158.     cleanup();
  159.     fclose(f);
  160.     return;
  161.     }
  162. /* read it all in */
  163.   cnt1=fread(diskbuf,1,bufinfo.size,f);
  164. /* one | is deliberate here also (see saveit(), above */
  165.   if (ferror(f)|fclose(f)|(cnt1!=bufinfo.size))
  166.     {
  167.     advise("Error reading file");
  168.     cleanup();
  169.     return;
  170.     }
  171.   checksum();
  172.   strcpy(bufinfo.source,fn);
  173.   bufinfo.copies=0;
  174.   wait_off();
  175.   }
  176.  
  177.